package wire

import (
	"fmt"

	"github.com/garyburd/redigo/redis"
	"github.com/gookit/color"
	"github.com/xormplus/xorm"
	"google.golang.org/grpc"

	"gitee.com/krio/grpc-go-server/cmd/lib"
	"gitee.com/krio/helper/logger"
)

type App struct {
	Config *lib.Config    // 配置
	Logger *logger.Logger // 日志
	Redis  *redis.Pool    // redis
	Dao    *xorm.Engine   // mysql
	GRPC   *grpc.Server   // grpc
}

func (a *App) Run() error {
	cmdPrint()
	return lib.GRPCServer(a.GRPC)
}

func InitLogger() *logger.Logger {
	lc := lib.C.Logger
	return lib.InitLog(&lib.Log{
		Level:    lc.Level,
		FilePath: lc.FilePath,
		Debug:    lc.Debug,
		TraceKey: lc.TraceKey,
	})
}

// cmd打印参数
func cmdPrint() {
	color.Greenp(fmt.Sprintf("*[ logger: %s ]\n", lib.C.Logger.Level))
	color.Greenp(fmt.Sprintf("*[ redis: %s/%d ]\n", lib.C.Redis.Dsn, lib.C.Redis.DB))
	color.Greenp(fmt.Sprintf("*[ mysql: %s ]\n", lib.C.Mysql.URL))
	color.Greenp(fmt.Sprintf("*[ grpc: %d ]\n", lib.C.GRPC.Port))
}
